﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL.DAL;
using XQ.WeChatOfficialAccount.DAL;
using XQ.Framework.core;

namespace XQ.WeChatOfficialAccount.BLL.Account
{
    public class RoleBll
    {
        public List<Sys_Roles> GetRoles()
        {
            using (RoleDal dal = new RoleDal())
            {
                return dal.FindAllModel(x => x.ID > -1).OrderByDescending(x => x.ID).OrderBy(x => x.Sortnum).ToList();
            }
        }

        public Sys_Roles GetRole(int id)
        {
            using (RoleDal dal = new RoleDal())
            {
                return dal.FindModel(x => x.ID == id);
            }
        }

        public string Delete(int id)
        {
            using (RoleDal dal = new RoleDal())
            {
                return dal.DeleteModel(x => x.ID == id) > 0 ? "ok" : "删除错误";
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        public bool DeleteByIds(List<int> ids)
        {
            using (RoleDal dal = new RoleDal())
            {
                return dal.DeleteByIds(ids);
            }
        }

        public string Save(Sys_Roles role)
        {
            if (role == null) { return "model is null"; }
            if (!ValidateHelper.IsPlumpString(role.RoleName))
            {
                return "角色名不能为空";
            }
            using (RoleDal dal = new RoleDal())
            {
                if (role.ID > 0)
                {
                    if (dal.ExistModel(x => x.ID != role.ID && x.RoleName == role.RoleName))
                    {
                        return "存在同名角色";
                    }
                    //update
                    return dal.UpdateModel(x => x.ID == role.ID, (model) =>
                    {
                        model.RoleName = role.RoleName;
                        model.Sortnum = role.Sortnum;
                        model.Remark = role.Remark;
                        model.isDefault = role.isDefault;
                        return model;
                    }) > 0 ? "ok" : "保存失败";
                }
                else
                {
                    if (dal.ExistModel(x => x.RoleName == role.RoleName))
                    {
                        return "存在同名角色";
                    }
                    Sys_Roles newRole = dal.AddModel(role);
                    return newRole.ID > 0 ? "ok" : "添加失败";
                }
            }
        }

        /// <summary>
        /// 根据流程节点ID获取对应的角色
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public List<Sys_Roles> GetRolesByNodeId(int nodeId)
        {
            using (RoleDal dal = new RoleDal())
            {
                return dal.GetRolesByNodeId(nodeId);
            }
        }

        /// <summary>
        /// 根据用户ID获取权限ID
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int GetRoleIDByUserID(int userid)
        {
            using (RoleDal dal = new RoleDal())
            {
                Sys_Roles role = dal.GetRoleByUserId(userid);
                if (role != null)
                {
                    return role.ID;
                }
            }
            return 0;
        }
    }
}
